//
//  MenuControlView.m
//  B段项目自定义
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//

#import "MenuControlView.h"
static NSString *identifier = @"menuControlViewCell";

@interface MenuControlView ()
@property(nonatomic, assign)CGRect rect;
@property(nonatomic, assign)NSIndexPath *selectedindexPath;
@property(nonatomic, assign)BOOL isReload;
@property(nonatomic, strong)LabelCollectionViewCell *seletedCell;
@property(nonatomic, assign)CGSize itemSize;
@end
@implementation MenuControlView

-(UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, _rect.size.width, _rect.size.height) collectionViewLayout:flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView NightWithType:UIViewColorTypeNormal];
        [_collectionView registerClass:[LabelCollectionViewCell class] forCellWithReuseIdentifier:identifier];
        [self addSubview:_collectionView];
    }
    return _collectionView;
}

-(void)setDataSource:(NSArray *)dataSource
{
    _dataSource = dataSource;
    
    if (_itemSize.width == 0) {
        _itemSize = CGSizeMake(FitConstant_XorWidth(60), FitConstant_YorHeight(40));
    }
    [self.collectionView reloadData];
}

-(void)setNumberOfMenu:(NSInteger)numberOfMenu
{
    _numberOfMenu = numberOfMenu;
    
    _itemSize = CGSizeMake((ScreenWidth) / numberOfMenu, FitConstant_YorHeight(40));
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _rect = frame;
    }
    return self;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (_dataSource.count == 0) {
        return 0;
    }
    return _dataSource.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LabelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    
    cell.contentLabel.text = _dataSource[indexPath.row];
    if (indexPath.row == 0 && _seletedCell == nil) {
        _seletedCell = cell;
        _selectedindexPath = indexPath;
        [_seletedCell changeCellColor:YES];
    }
    if (_selectedindexPath == indexPath) {
        [cell changeCellColor:YES];
    }
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LabelCollectionViewCell *cell = (LabelCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    LabelCollectionViewCell *firstCell = (LabelCollectionViewCell *)[collectionView cellForItemAtIndexPath:_selectedindexPath];
    firstCell.contentLabel.textColor = [UIColor blackColor];
    cell.contentLabel.textColor = [UIColor orangeColor];
    [_seletedCell changeCellColor:NO];
    _seletedCell = cell;
    _selectedindexPath = indexPath;
    [_seletedCell changeCellColor:YES];
    
    [self.delegate didSelectedMenu:indexPath.row];
}

#pragma mark 将要显示
-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_selectedindexPath == indexPath) {
        _seletedCell = (LabelCollectionViewCell *)cell;
        [_seletedCell changeCellColor:YES];
    }
}

#pragma mark 结束显示
-(void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (cell == _seletedCell) {
        [_seletedCell changeCellColor:NO];
    }
}

-(void)selectCell:(NSInteger)number
{
    [_seletedCell changeCellColor:NO];
    _selectedindexPath = [NSIndexPath indexPathForRow:number inSection:0];
    _seletedCell = (LabelCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:_selectedindexPath];
    [_collectionView reloadData];
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
//    //NSLog(@"%.2f", _itemSize.width);
    return _itemSize;
}

@end
